package com.natasha.pins.bh.pinsmaster;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.natasha.pins.component.database.DBHandler;
import com.natasha.pins.data.bean.PinsMasterBean;
import com.natasha.pins.data.dao.PinsMasterDao;
import com.natasha.pins.util.Constants;


public class PinsMasterBH {
	
	private Context context;
	private DBHandler dbHandler;
	private SQLiteDatabase connection;
	private PinsMasterDao pinsMasterDao;
	private List<PinsMasterBean> pinsMasterBeanList;
	
	public PinsMasterBH(Context context){
		this.context = context;
		dbHandler = new DBHandler(context);
		getConnection();
		pinsMasterDao = new PinsMasterDao(connection);
	}
	
	public void getConnection(){
		this.connection = dbHandler.getConnection();
	}
	
	public void closeConnection(){
		dbHandler.close();
	}
	
	public long createPin(PinsMasterBean pinsMasterBean){
		long result;
		
		result = pinsMasterDao.createPin(pinsMasterBean);

		Log.i(Constants.LOG_DEBUG, this.getClass().getName() + " Create Pin result: " + result);
		return result;
	}
	
	public boolean fetchAllPins(){
		pinsMasterBeanList = new ArrayList<PinsMasterBean>();
		Cursor cursor;
		cursor = pinsMasterDao.fetchAllPins();
		
		if(cursor!=null){
			Log.i(Constants.LOG_DEBUG, this.getClass().getName() + " fetchAllPins rows: " + cursor.getCount());
			if (cursor.moveToFirst()) {
				do {
					pinsMasterBeanList.add(new PinsMasterBean(cursor));
				} while (cursor.moveToNext());
			}
		}
		else{
			Log.i(Constants.LOG_DEBUG, this.getClass().getName() + " fetchAllPins no rows selected ");
		}
		
		
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		
		return true;
	}
	
	public Cursor fetchPin(long rowId){
		Cursor cursor;
		
		cursor = pinsMasterDao.fetchPin(rowId);
		if(cursor!=null){
			Log.i(Constants.LOG_DEBUG, this.getClass().getName() + " fetchPin row: " + cursor.getCount());
		}
		else{
			Log.i(Constants.LOG_DEBUG, this.getClass().getName() + " fetchPin no rows selected ");
		}
		return cursor;
	}

	public boolean updatePin(PinsMasterBean newPin){
		long result;
		
		Log.d(Constants.LOG_DEBUG, this.getClass().getName() + ".updatePin - Attempting to update pinId: " + newPin.getPmid());
		
		result = pinsMasterDao.updatePin(newPin);
		Log.d(Constants.LOG_DEBUG, this.getClass().getName() + " Update Pin result: " + result);
		return result>0;
	}
	
	public boolean deletePin(long id){
		long result;
		result = pinsMasterDao.deletePin(id);
		Log.i(Constants.LOG_DEBUG, this.getClass().getName() + " Delete Pin result: " + result);
		return result>0;
	}
	
	public boolean deleteAllPins(){
		long result;
		result = pinsMasterDao.deleteAllPins();
		Log.i(Constants.LOG_DEBUG, this.getClass().getName() + " Delete all pins, rows deleted: " + result);
		return result>0;
	}

	public List<PinsMasterBean> getPinsMasterBeanList() {
		return pinsMasterBeanList;
	}

	public void setPinsMasterBeanList(List<PinsMasterBean> pinsMasterBeanList) {
		this.pinsMasterBeanList = pinsMasterBeanList;
	}
}
